package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.xiaoymin.knife4j.core.util.CollectionUtils;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealOverViewVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;

@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {
    @Resource
    private SetmealMapper setmealMapper;
    @Resource
    private SetmealDishMapper setmealDishMapper;

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult selectSetmeal(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.selectSetmeal(setmealPageQueryDTO);
        return PageResult.builder().total(page.getTotal()).records(page.getResult()).build();
    }

    /**
     * 添加套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void insertSetMeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //添加套餐
        setmealMapper.insertSetMeal(setmeal);
        Long id = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(CollectionUtils.isNotEmpty(setmealDishes)){//判断集合非空
            //forEach遍历插入套餐id
            setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(id));
            log.info("666 :{}", setmealDishes );
            //添加套餐菜品关联
            setmealDishMapper.insertSetMealDish(setmealDishes);
        }
    }

    /**
     * 删除套餐
     * @param ids
     */
    @Override
    @Transactional
    public void deleteSetmeal(List<Long> ids) {
        if(setmealMapper.statusCount(StatusConstant.ENABLE,ids) > 0){
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }
        setmealDishMapper.deleteSetmaelDish(ids);
        setmealMapper.deleteSetmeal(ids);
    }

    /**
     * 根据id查询套餐信息
     * @param id
     * @return
     */
    @Override
    public SetmealVO selectSetmealById(Long id) {
        SetmealVO setmealVO = setmealMapper.selectSetmealById(id);
        setmealVO.setSetmealDishes(setmealDishMapper.selectSetmealDishesById(id));
        return setmealVO;
    }

    /**
     * 修改套餐信息
     * @param setmealVO
     */
    @Override
    public void updateSetMeal(SetmealVO setmealVO) {
        List<SetmealDish> setmealDishes = setmealVO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealVO.getId()));
        if(CollectionUtils.isNotEmpty(setmealDishes)){
            List<Long> list = new ArrayList<>();
            list.add(setmealVO.getId());
            setmealDishMapper.deleteSetmaelDish(list);
            setmealDishMapper.insertSetMealDish(setmealDishes);
        }
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealVO,setmeal);
        setmealMapper.updateSetMeal(setmeal);
    }

    /**
     * 起售停售
     * @param status
     * @param id
     */
    @Override
    public void updateStatus(Integer status, Long id) {
        if(status.equals(StatusConstant.ENABLE ) && setmealDishMapper.selectStatusCount(StatusConstant.DISABLE,id) > 0){
            throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
        }
        setmealMapper.updateSetMeal(Setmeal.builder().status(status).id(id).build());
    }
    /**
     * 根据分类ID查询套餐
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        return setmealMapper.list(setmeal);
    }
    /**
     * 根据套餐id查询包含的菜品列表
     *
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
